package com.cafe.case8.client;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.cafe.case8.request.BaseRequest;
import com.cafe.case8.request.GoodsDetailRequest;
import com.cafe.case8.request.GoodsListRequest;
import com.cafe.case8.request.OrderDetailRequest;
import com.cafe.case8.response.BaseResponse;
import com.cafe.case8.response.GoodsDetailResponse;
import com.cafe.case8.response.GoodsListResponse;
import com.cafe.case8.response.OrderDetailResponse;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class JdClient {


    private String postRequest(String param) {
        return HttpUtil.createPost("https://api.m.jd.com/api").body(param).execute().body();
    }


    public <T extends BaseResponse> T execute(BaseRequest<T> request) {
        String result = postRequest(JSONUtil.toJsonStr(request));
        return JSONUtil.toBean(result, request.getResponseType());
    }

    public static void main(String[] args) {
        JdClient  jdClient = new JdClient();
        GoodsListRequest request = new GoodsListRequest();
        request.setGoodsIds(List.of("10001"));

        GoodsListResponse goodsListResponse = jdClient.execute(request);

        GoodsDetailRequest  goodsDetailRequest = new GoodsDetailRequest();
        GoodsDetailResponse goodsDetailResponse = jdClient.execute(goodsDetailRequest);


        OrderDetailRequest orderDetailRequest = new OrderDetailRequest();
        orderDetailRequest.setOrderId("10001");
        OrderDetailResponse orderDetailResponse = jdClient.execute(orderDetailRequest);


    }

}
